package ld.oauth.blog.model.po.user;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.google.common.collect.Lists;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 用户表
 * </p>
 *
 * @author
 * @since 2022-08-29
 */
@Getter
@Setter
@TableName("sys_user")
@ApiModel(value = "SysUser对象", description = "用户表")
@ToString
public class SysUser implements UserDetails {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty("用户 ID")
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    @ApiModelProperty("用户名")
    private String username;

    @ApiModelProperty("密码，加密存储, admin/1234")
    private String password;

    @ApiModelProperty("帐户是否过期(1 未过期，0已过期)")
    private Boolean isAccountNonExpired;

    @ApiModelProperty("帐户是否被锁定(1 未过期，0已过期)")
    private Boolean isAccountNonLocked;

    @ApiModelProperty("密码是否过期(1 未过期，0已过期)")
    private Boolean isCredentialsNonExpired;

    @ApiModelProperty("帐户是否可用(1 可用，0 删除用户)")
    private Boolean isEnabled;

    @ApiModelProperty("昵称")
    private String nickName;

    @ApiModelProperty("注册手机号")
    private String mobile;

    @ApiModelProperty("注册邮箱")
    private String email;

    private LocalDateTime createDate;

    private LocalDateTime updateDate;

    private String imageUrl;

    @TableField(exist=false)
    private Collection<? extends GrantedAuthority> authorities;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    @Override
    public boolean isAccountNonExpired() {
        return this.isAccountNonExpired;
    }

    @Override
    public boolean isAccountNonLocked() {
        return this.isAccountNonLocked;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return this.isCredentialsNonExpired;
    }

    @Override
    public boolean isEnabled() {
        return this.isEnabled;
    }

    @TableField(exist = false)
    private List<SysRole> roleList = Lists.newArrayList();

    @TableField(exist = false)
    private List<Long> roleIds = Lists.newArrayList();

    @TableField(exist = false)
    private List<SysPermission> permissionList = Lists.newArrayList();

    public List<Long> getRoleList() {
        if(!CollectionUtils.isEmpty(roleList)){
            roleIds = Lists.newArrayList();
            roleList.stream().forEach(sysRole -> {
                roleIds.add(sysRole.getId());
            });
        }
        return roleIds;
    }

}
